package com.wffz.comm.idTools;

import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.NetworkInterface;

public class ShortSnowflakeGenerator{
    private final long twepoch = 1511951129000L;
    private final long workerIdBits = 1L;
    private final long datacenterIdBits = 1L;
    private final long maxWorkerId = 1L;
    private final long maxDatacenterId = 1L;
    private final long sequenceBits = 1L;
    private final long workerIdShift = 1L;
    private final long datacenterIdShift = 2L;
    private final long timestampLeftShift = 3L;
    private final long sequenceMask = 1L;
    private long workerId;
    private long datacenterId;
    private long sequence = 0L;
    private long lastTimestamp = -1L;

    public ShortSnowflakeGenerator() {
        this.datacenterId = getDatacenterId(1L);
        this.workerId = getMaxWorkerId(this.datacenterId, 1L);
    }

    public ShortSnowflakeGenerator(long workerId, long datacenterId) {
        if (workerId <= 1L && workerId >= 0L) {
            if (datacenterId <= 1L && datacenterId >= 0L) {
                this.workerId = workerId;
                this.datacenterId = datacenterId;
            } else {
                throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", 1L));
            }
        } else {
            throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", 1L));
        }
    }

    public synchronized long nextId() {
        long timestamp = this.timeGen();
        if (timestamp < this.lastTimestamp) {
            throw new RuntimeException(String.format("Clock moved backwards.  Refusing to generate id for %d milliseconds", this.lastTimestamp - timestamp));
        } else {
            if (this.lastTimestamp == timestamp) {
                this.sequence = this.sequence + 1L & 1L;
                if (this.sequence == 0L) {
                    timestamp = this.tilNextMillis(this.lastTimestamp);
                }
            } else {
                this.sequence = 0L;
            }

            this.lastTimestamp = timestamp;
            return timestamp - 1511951129000L << 3 | this.datacenterId << 2 | this.workerId << 1 | this.sequence;
        }
    }

    protected long tilNextMillis(long lastTimestamp) {
        long timestamp;
        for(timestamp = this.timeGen(); timestamp <= lastTimestamp; timestamp = this.timeGen()) {
        }

        return timestamp;
    }

    protected long timeGen() {
        return System.currentTimeMillis();
    }

    protected static long getDatacenterId(long maxDatacenterId) {
        long id = 0L;

        try {
            InetAddress ip = InetAddress.getLocalHost();
            NetworkInterface network = NetworkInterface.getByInetAddress(ip);
            if (network == null) {
                id = 1L;
            } else {
                byte[] mac = network.getHardwareAddress();
                if (null != mac) {
                    id = (255L & (long)mac[mac.length - 1] | 65280L & (long)mac[mac.length - 2] << 8) >> 6;
                    id %= maxDatacenterId + 1L;
                }
            }
        } catch (Exception var7) {
            System.err.println(" getDatacenterId: " + var7.getMessage());
        }

        return id;
    }

    protected static long getMaxWorkerId(long datacenterId, long maxWorkerId) {
        StringBuilder mpid = new StringBuilder();
        mpid.append(datacenterId);
        String name = ManagementFactory.getRuntimeMXBean().getName();
        if (name != null && "".equals(name)) {
            mpid.append(name.split("@")[0]);
        }

        return (long)(mpid.toString().hashCode() & '\uffff') % (maxWorkerId + 1L);
    }

    public static void main(String[] args) throws InterruptedException {
        int max = 100;
        ShortSnowflakeGenerator idWorker = new ShortSnowflakeGenerator();

        for(int i = 0; i < max; ++i) {
            Long id = idWorker.generator();
            System.out.println(id + "\n");
        }

    }

    public Long generator() {
        long id = this.nextId();
        return id;
    }
}
